package com.augurit.gzcgwsp.ljzyz.web;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.ljzyz.convert.YwYnBusinfoConvertor;
import com.augurit.gzcgwsp.ljzyz.entity.YwYnBusinfo;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.wf.common.util.WfUtils;
import com.google.gson.Gson;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-yn-businfo.action",type = "redirect"),
	@Result(name = "reloadDelBus", location = "yw-yn-businfo!listDeleteBus.action",
			params={"qyid","${qyid}"},type = "redirect"),
	@Result(name ="toListClxx", location = "yw-yn-businfo!listClxxByQyid.action",
			params={"qyid","${qyid}"},type = "redirect"),
	@Result(name ="toListNsdj", location = "yw-yn-businfo!nssyDj.action",
					params={"qyid","${qyid}"},type = "redirect"),
					
					
	@Result(name = "viewBusInfo", location = "/gzcgwsp/ywcmn/YwComClxx/viewBusInfo.jsp"),	
	@Result(name = "tosyyxq", location = "/gzcgwsp/ywcmn/YwComClxx/listsyyxq.jsp"),	
	@Result(name = "stateBusInfo", location = "/gzcgwsp/ywcmn/YwComClxx/listStateBusInfo.jsp"),	
	@Result(name ="findBusData", location = "/gzcgwsp/ywcmn/YwComClxx/listBusData.jsp"),
    @Result(name ="toListCldj", location = "/gzcgwsp/ywcmn/YwComClxx/YwComClxx.jsp"),
	@Result(name ="toNssy", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-nssy.jsp"),
	@Result(name ="listClxx", location = "/gzcgwsp/ywcmn/YwComClxx/listClxx.jsp"),
	@Result(name ="listDeleteBus", location = "/gzcgwsp/ywcmn/YwComClxx/listDeleteBus.jsp"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/ywcmn/YwComClxx/YwComClxx.jsp"),
	@Result(name = "tonsdj", location = "/gzcgwsp/ywcmn/YwComClxx/toNsdj.jsp"),
	@Result(name = "listNsClxx", location = "/gzcgwsp/ywcmn/YwComClxx/listNsClxx.jsp"),
	@Result(name = "nssyDj", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-nssyDj.jsp"),
	@Result(name = "toAddCar", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-addCar.jsp"),
	@Result(name = "toCar", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-car.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/ywcmn/YwComClxx/YwComClxx-input.jsp"),
	@Result(name = "nssyList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-nssyList.jsp"),
	@Result(name = "hfqkList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-hfqkList.jsp"),
	@Result(name = "hfqkEdit", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-hfqkEdit.jsp"),
	@Result(name = "kspList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-kspList.jsp"),
	@Result(name = "kspEdit", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-kspEdit.jsp"),
	@Result(name = "fgcspList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-fgcspList.jsp"),
	@Result(name = "fgcspEdit", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-fgcspEdit.jsp"),
	@Result(name = "cspList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-cspList.jsp"),
	@Result(name = "findbus", location = "/gzcgwsp/ywcmn/YwComClxx/findbus.jsp"),
	@Result(name = "cspEdit", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-cspEdit.jsp")
})
public class YwYnBusinfoAction extends CrudActionSupport<YwYnBusinfoForm>{

	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
    @Autowired
    private ISysCodeService syscodeService;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
	private YwYnBusinfoForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<YwYnBusinfoForm> page = new Page<YwYnBusinfoForm>(20);
	private String params;
	private String ajid;
	private String qyid;
	private Long busInfoId;
	private String shjg;
	private String spsx;
	private String bfyy;
	
	
	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		ywYnBusinfoService.search(page, form);
		getRequest().setAttribute("qyxx", qyxx);
		return SUCCESS;
	}
	/*
	 * 车辆登记
	 */
	public String toListCldj() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		ywYnBusinfoService.search(page, form);
		getRequest().setAttribute("qyxx", qyxx);
		return "toListCldj";
	}
	
	/**
	 * 到年审审验
	 * @return
	 * @throws Exception
	 */
	public String tonsdj() throws Exception {
		return "tonsdj";
	}
	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		List<SysCodeForm> cxList = syscodeService.getItems("YW_YN_CX");
        getRequest().setAttribute("cxList", cxList);
		List<YwYnBusinfoForm> businfoList = ywYnBusinfoService.search(page, form).getResult();
		System.out.println("form.getGsmc() = "+form.getGsmc());
		return INPUT;
	}

	/**
	 * 查看申请资料
	 * @return
	 * @throws Exception
	 */
	public String viewBusInfo() throws Exception{
		List<YwYnBusinfoForm> list=this.ywYnBusinfoService.getSpData(id,null);
		getRequest().setAttribute("list",list);
		getRequest().setAttribute("curYear",DateUtilsEX.getCurYear());
		getRequest().setAttribute("loginUser", this.getLoginUser().getUser().getUserName());

		return "viewBusInfo";
	}
	
	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save()  {

		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		
		try{
		JSONObject jsonObject = JSONObject.fromObject(params);
		 Map<String, String> map = new HashMap<String, String>();
		 for(Object k : jsonObject.keySet()){//把json里面的空日期去掉
	            String val = jsonObject.get(k).toString(); 
	              if(StringUtils.isNotBlank(val)){
	            	  map.put(k.toString(), val);  
	              }
	        }
		 jsonObject=JSONObject.fromObject(map);
		 
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy年MM月dd日"}) );
		YwYnBusinfo busInfo=(YwYnBusinfo) JSONObject.toBean(jsonObject, YwYnBusinfo.class);
		YwYnBusinfoForm businfoForm=YwYnBusinfoConvertor.convertVoToForm(busInfo);
		String busInfoId = this.getRequest().getParameter("id");
		if(StringUtils.isNotEmpty(busInfoId)){//修改
			busInfo.setId(Long.parseLong(busInfoId));
			ywYnBusinfoService.update(busInfo);
		}else{//新增
			ywYnBusinfoService.save(businfoForm);
		}		
		response.getWriter().write("success");
		}catch(Exception e){
			try {
				response.getWriter().write("请检查输入内容长度是否过大!");
			} catch (IOException e1) {				
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 到新增车辆资料登记
	 * @return
	 * @throws Exception
	 */
	public String toAddCar() throws Exception {
		String qyid=getRequest().getParameter("qyid");
		 List<SysCodeForm> cxs = syscodeService.getItems("YW_CLLX");
	     getRequest().setAttribute("cx", cxs);
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		getRequest().setAttribute("qyxx", qyxx);
		if(form.getCphm()==null){
			form.setCphm("粤A");
		}
		//车辆编码
		if(form.getClbh()==null && StringUtils.isNotBlank(qyid)){
			YwComQyxxForm qyform=ywComQyxxService.get(Long.valueOf(qyid));
			//写上标识代码
			form.setClbh(qyform.getZydm());
		}
		if(form.getCllx()==null){
			form.setCllx("运余泥");
		}
		if(qyid==null){
			return "toCar";
		}
		return "toAddCar";
	}
	
	/**
	 * 统计报表
	 * @return
	 */
	public String stateBusInfo(){
		Map map=getRequest().getParameterMap();
		List<String[]> list=ywYnBusinfoService.stateBusInfo(map);
		getRequest().setAttribute("list",list);
		return "stateBusInfo";
	}
	
	
	/**
	 * 保存新增车辆资料
	 * @return
	 * @throws Exception
	 */
	public String saveBusinfo() throws Exception {
		if(form.getGsmc()!=null){
			form.setQyid(Long.valueOf(form.getGsmc()));
			form.setGsmc("");
		}
		String length=getRequest().getParameter("length");
		String width=getRequest().getParameter("width");
		String height=getRequest().getParameter("height");
		
		String intnumber=getRequest().getParameter("intnumber");
		String decimalnumber=getRequest().getParameter("decimalnumber");
		if(StringUtils.isNotBlank(length) && StringUtils.isNotBlank(width) &&StringUtils.isNotBlank(height)){
			String hxnbcc=length+"*"+width+"*"+height;
			form.setHxnbcc(hxnbcc);//设置货箱内部尺寸
			
		}
		form.setIsNewBus("1");
		form.setZzl(intnumber+"."+decimalnumber);//设置载重量
		ywYnBusinfoService.save(form);
		return "toListClxx";
	}
	
	/**
	 * 保存新增车辆资料
	 * @return
	 * @throws Exception
	 */
	public String saveBus() throws Exception {
		String length=getRequest().getParameter("length");
		String width=getRequest().getParameter("width");
		String height=getRequest().getParameter("height");
		if(StringUtils.isNotBlank(length) && StringUtils.isNotBlank(width)&& 
				StringUtils.isNotBlank(height) ){
			String hxnbcc=length+"*"+width+"*"+height;
			form.setHxnbcc(hxnbcc);//设置货箱内部尺寸
		}
		
		String intnumber=getRequest().getParameter("intnumber");
		String decimalnumber=getRequest().getParameter("decimalnumber");
		form.setZzl(intnumber+"."+decimalnumber);//设置载重量
		ywYnBusinfoService.saveForm(form);
		return CrudActionSupport.RELOAD;
	}
	/**
	 * 车辆登记的时候保存新增车辆资料
	 * @return
	 * @throws Exception
	 */
	public String saveCar() throws Exception {
		if(form.getGsmc()!=null){
			form.setQyid(Long.valueOf(form.getGsmc()));
			form.setGsmc("");
		}
		ywYnBusinfoService.save(form);
		return "toListClxx";
	}
	/**
	 * 显示企业的所有车辆信息列表
	 * @return
	 * @throws Exception
	 */
	public String listClxxByQyid() throws Exception {
		form.setGsmc("");
		List<YwComQyxx> clxx=ywYnBusinfoService.getQyxx();
		ywYnBusinfoService.search(page, form);
		getRequest().setAttribute("qyxx", clxx);
		return "listClxx";
	}
	
	/**
	 * 到年审审验页面
	 * @return
	 */
	public String toNssy(){
		return "toNssy";
	}
	
	/**
	 * 编辑车辆信息（后台）
	 * @return
	 * @throws Exception
	 */
	public String editBusinfo() throws Exception {
		List<SysCodeForm> cxList = syscodeService.getItems("YW_CLLX");
        getRequest().setAttribute("cxList", cxList);
        //加载载重量
        if(StringUtils.isNotBlank(form.getZzl())){
        	String[] zzls=form.getZzl().split("[.]");
        	if(zzls.length>1){
        		getRequest().setAttribute("intnumber", zzls[0]);
            	getRequest().setAttribute("decimalnumber", zzls[1]);
        	}
        	
        }
        
        if(StringUtils.isNotBlank(form.getHxnbcc())){
        	//货箱内部尺寸
        	String hxnbcc=form.getHxnbcc();
        	String[] strs=hxnbcc.split("[*]");
        	if(strs.length>2){
        		getRequest().setAttribute("length", strs[0]);
            	getRequest().setAttribute("width", strs[1]);
            	getRequest().setAttribute("height", strs[2]);
        	}
        
        }
        
		if (id != null) {
			form = ywYnBusinfoService.get(id);
		} else {
			form = new YwYnBusinfoForm();
		}
		return INPUT;
	}
	/**
	 * 年审审验列表（后台）
	 * @return
	 * @throws Exception
	 */
	public String nssyList() throws Exception {
		if(StringUtils.isNotBlank(form.getGsmc())){
			form.setQyid(Long.valueOf(form.getGsmc()));
		}
		String findqyid=getParameter("findqyid");
		if(StringUtils.isBlank(findqyid)){//不是页面查找
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","1");
			form.setQyid(qyid);
		}else{
			form.setQyid(Long.valueOf(findqyid));
		}
	
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.getCarNsdj(page, form);
		return "nssyList";
	}
	
	/**
	 * 年审登记显示列表页面
	 * @return
	 * @throws Exception
	 */
	public String nssyDj() throws Exception {
		if(StringUtils.isNotBlank(form.getGsmc())){
			form.setQyid(Long.valueOf(form.getGsmc()));
		}else{
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","1");
			form.setQyid(qyid);
		}
		
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		ywYnBusinfoService.getCarNsdj(page, form);
		getRequest().setAttribute("qyxx", qyxx);
		return "nssyDj";
	}
	
	/**
	 * 登记车辆
	 * @return
	 */
	public String doNsDj() throws Exception {
		String isDelete=getRequest().getParameter("delete");
		YwYnBusinfoForm form = ywYnBusinfoService.get(id);
		if(form!=null && form.getId()!=null){
			if("true".equals(isDelete)){
				form.setNssts("0");//表示还没开始年审
				this.ywYnBusinfoService.save(form);
			}else {
				form.setNssts("1");//开始年审
				this.ywYnBusinfoService.save(form);
			}
		}
		return "toListNsdj";//this.nssyDj();
	}
	
	/**
	 * 年审审验
	 * @return
	 * @throws Exception
	 */
	public String nssy() throws Exception{
		if(StringUtils.isNotBlank(form.getGsmc())){
			form.setQyid(Long.valueOf(form.getGsmc()));
		}
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		ywYnBusinfoService.getCarNsdj(page, form);
		getRequest().setAttribute("qyxx", qyxx);
		return "nssy";
	}
	

	
	
	/**
	 * 核发情况列表（后台）
	 * @return
	 * @throws Exception
	 */
	public String hfqkList() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		String findqyid=getParameter("findqyid");
		if(StringUtils.isBlank(findqyid)){//不是页面查找
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","2");
			form.setQyid(qyid);
		}else{
			form.setQyid(Long.valueOf(findqyid));
		}
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.getNshf(page, form);
		return "hfqkList";
	}
	
	/**
	 * 核发情况编辑（后台）
	 * @return
	 * @throws Exception
	 */
	public String hfqkEdit() throws Exception {
		form = ywYnBusinfoService.sp(id,"1","2");
		
		form.setId(id);
		if(form.getYxksrq()==null){
			form.setYxksrq(new Date());
		}
		if(form.getYxjsrq()==null){
			Date date=DateUtilsEX.getDateByMonth(form.getYxksrq(), 6,true);
			form.setYxjsrq(date);
		}
		 //查询本年度以前的年审过的信息
		List<YwYnBusinfoForm> list=this.ywYnBusinfoService.getSpData(id,null);
		getRequest().setAttribute("list",list);
		getRequest().setAttribute("curYear",DateUtilsEX.getCurYear());
		getRequest().setAttribute("loginUser", this.getLoginUser().getUser().getUserName());
		return "hfqkEdit";
	}
	
	/**
	 * 科审批列表（后台）
	 * @return
	 * @throws Exception
	 */
	public String kspList() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		Long[] roleIds =getLoginUser().getRoleIds();//角色id
		String findqyid=getParameter("findqyid");
		if(StringUtils.isBlank(findqyid)){//不是页面查找
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","3");
			form.setQyid(qyid);
		}else{
			form.setQyid(Long.valueOf(findqyid));
		}
		
		for(Long roleId:roleIds){
			if(roleId==145){//表示科长审批
				getRequest().setAttribute("issp", "true");
				break;
			}
		}
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.getspList(page, form,"2","3");
		return "kspList";
	}
	
	/**
	 * 科审批编辑（后台）
	 * @return
	 * @throws Exception
	 */
	public String ksp() throws Exception {
		form = ywYnBusinfoService.sp(id,"2","2");
		if(form!=null){
			form.setKspr(getLoginUser().getUser().getUserName());
		}
	    //查询以前的年审过的信息
		List<YwYnBusinfoForm> list=ywYnBusinfoService.getSpData(id,null);
		getRequest().setAttribute("list",list);
		getRequest().setAttribute("curYear",DateUtilsEX.getCurYear());
		getRequest().setAttribute("loginUser", getLoginUser().getUser().getUserName());
		return "kspEdit";
	}
	
	/**
	 * 分管处审批列表（后台）
	 * @return
	 * @throws Exception
	 */
	public String fgcspList() throws Exception {
		String findqyid=getParameter("findqyid");
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		Long[] roleIds =getLoginUser().getRoleIds();//角色id
		if(StringUtils.isBlank(findqyid)){//不是页面查找
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","4");
			form.setQyid(qyid);
		}else{
			form.setQyid(Long.valueOf(findqyid));
		}
		for(Long roleId:roleIds){
			if(roleId==144){//表示分处审批
				getRequest().setAttribute("issp", "true");
				break;
			}
		}
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.getspList(page, form,"2","4");
		return "fgcspList";
	}
	
	/**
	 * 分管处审批编辑（后台）
	 * @return
	 * @throws Exception
	 */
	public String fgcsp() throws Exception {
		form = ywYnBusinfoService.sp(id,"2","2");
		getRequest().setAttribute("loginUser", this.getLoginUser().getUser().getUserName());
		return "fgcspEdit";
	}
	
	/**
	 * 处审批列表（后台）
	 * @return
	 * @throws Exception
	 */
	public String cspList() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		Long[] roleIds =getLoginUser().getRoleIds();//角色id
		String findqyid=getParameter("findqyid");
		if(StringUtils.isBlank(findqyid)){//不是页面查找
			//默认显示最大车辆id的公司
			Long qyid=this.ywYnBusinfoService.getBusQyid("1","5");
			form.setQyid(qyid);
		}else{
			form.setQyid(Long.valueOf(findqyid));
		}
		
		for(Long roleId:roleIds){
			if(roleId==143){//表示处审批
				getRequest().setAttribute("issp", "true");
				break;
			}
		}
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.getspList(page, form,"2","5");
		return "cspList";
	}
	
	/**
	 * 处审批编辑（后台）
	 * @return
	 * @throws Exception
	 */
	public String csp() throws Exception {
		form = ywYnBusinfoService.sp(id,"2","2");
		getRequest().setAttribute("loginUser", this.getLoginUser().getUser().getUserName());
		return "cspEdit";
	}
	
	/**
	 * 修改审核结果
	 * @return
	 * @throws Exception
	 */
	public String edit()  {
		List<SysCodeForm> cxList = syscodeService.getItems("YW_YN_CX");
        getRequest().setAttribute("cxList", cxList);
		try {
			String busIdStr = this.getRequest().getParameter("busInfoId");
			if(StringUtils.isNotEmpty(busIdStr)){
				String[] busIdArr = busIdStr.split(",");
				for(int i=0;i<busIdArr.length;i++){
					if(StringUtils.isNotEmpty(busIdArr[i])){
						YwYnBusinfo busInfo=ywYnBusinfoService.findById(busIdArr[i]+"");
						if(StringUtils.isNotBlank(shjg)){//审核结果
							busInfo.setShjg(shjg);
						}
						if(StringUtils.isNotBlank(bfyy)){//报废原因
							busInfo.setBfyy(bfyy);
						}
						busInfo.setShr(this.getLoginUser().getUser().getUserName());//审核人
						Date date = new Date();
						YwYnBusinfoForm businfoForm=YwYnBusinfoConvertor.convertVoToForm(busInfo);
						businfoForm.setShrq(date);//审核日期
						Date zyzyxq = businfoForm.getZyzyxq();
						Date xszyxrq = businfoForm.getXszyxrq();
						Date minDate = null;
						if(xszyxrq != null && zyzyxq != null && "通过".equals(shjg)){//通过才有审核有效期
							boolean flag = zyzyxq.before(xszyxrq);
							if(flag){
								minDate = zyzyxq;
							}else{
								minDate = xszyxrq;
							}
							Calendar c =  Calendar.getInstance();
							c.setTime(date);//今天
							c.add(Calendar.MONTH, 6);//加6个月
					        Date afterAdd6Date = c.getTime();
					        if(afterAdd6Date.before(minDate)){//如果今天加6个月后的日期大于最小时间，则审核有效期为加6个月后的日期
					        	businfoForm.setShyxq(afterAdd6Date);//审核有效期
					        }else{//否则审核有效期为最小时间
					        	businfoForm.setShyxq(minDate);//审核有效期
					        }
						}
						ywYnBusinfoService.save(businfoForm);
					}					
				}
			}
			
		
		} catch (Exception e) {
			WfUtils.renderWfFailtureJson("请检查输入内容长度是否过大!");
		}
		return null;
	}
	
	/**
	 * 修改车辆所属的审批事项
	 * @return
	 * @throws Exception
	 */
	public String updateBusSpsx(){
		try {
			YwYnBusinfo busInfo=ywYnBusinfoService.findById(busInfoId+"");
			busInfo.setSpsx(spsx);
			YwYnBusinfoForm businfoForm=YwYnBusinfoConvertor.convertVoToForm(busInfo);			
//			ywYnBusinfoService.save(businfoForm);	
			ywYnBusinfoService.updateBusinfo(busInfoId,spsx);
			} catch (Exception e) {
				WfUtils.renderWfFailtureJson("请检查输入内容长度是否过大!");
			}
			return null;	
	}
	
	/**
	 * 根据案件id查找车辆列表
	 * @param ajid
	 * @return
	 * @throws IOException 
	 */
	public String findPageByAjid() throws IOException{                //修改为按企业ID查询
		String pageNo = this.getRequest().getParameter("pageNo");
		List<YwYnBusinfo> list=ywYnBusinfoService.findPageByAjid(qyid,pageNo);
//		System.out.println("pageNo = "+this.getRequest().getParameter("pageNo"));
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		Gson gson=new Gson();
		response.getWriter().write(gson.toJson(list));
		return null;
	}
	
	public String findById() throws IOException{
		
		YwYnBusinfo businfo=ywYnBusinfoService.findById(busInfoId+"");
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		Gson gson=new Gson();
		response.getWriter().write(gson.toJson(businfo));
		
		return null;
	}
	
	/**
	 * 车辆查询
	 */
	public String findBus() throws IOException{
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		this.getRequest().setAttribute("qyxx", qyxx);
		 return "findbus";
	}
	
	/**
	 * 查询车辆数据
	 * @return
	 * @throws IOException
	 */
	public String findBusData()throws IOException{
		String sfhg=this.getRequest().getParameter("sfhg");
		ywYnBusinfoService.findBusData(page, form,sfhg);
		//获取多少吨载重量
		String zzls=ywYnBusinfoService.findZzl(form);
		getRequest().setAttribute("zzls", zzls);
		return "findBusData";
	}
	/**
	 * 顺延有效期
	 * @return
	 * @throws Exception
	 */
    public String tosyyxq() throws Exception {
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		this.ywYnBusinfoService.searchSyyxq(page, form);
		getRequest().setAttribute("qyxx", qyxx);
        return "tosyyxq";
    }
	
	/**
	 * 删除的车辆显示
	 * @return
	 */
	public String listDeleteBus(){
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		getRequest().setAttribute("qyxx", qyxx);
		ywYnBusinfoService.searchDelBus(page, form);
		return "listDeleteBus";
	}
	
	
	/**
	 * 根据企业id查找车辆列表
	 * @param ajid
	 * @return
	 */
	public String findDataListByQyid() throws IOException{
		List<YwYnBusinfo> list = ywYnBusinfoService.findPageByQyid(String.valueOf(form.getQyid()));
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		Gson gson=new Gson();
		response.getWriter().write(gson.toJson(list));
		return null;
	}
	
	
	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		if(id != null){
			ywYnBusinfoService.deleteEntity(id);
			return RELOAD;
		}
		if(busInfoId != null){
			ywYnBusinfoService.deleteEntity(busInfoId);
			return null;
		}
		return null;
	}
	
	/**
	 * 从数据库删除多条记录
	 */
	public String deleteMore() throws Exception{
		for(Long id:checkedIds){
			ywYnBusinfoService.deleteEntity(id);
		}
		return RELOAD;
	}
	
	/**
	 * 从数据库删除多条记录
	 */
	public String deleteMoreBus() throws Exception{
		ywYnBusinfoService.delete(checkedIds);
		return "reloadDelBus";
	}
	
	/**
	 * 从数据库数据记录
	 * @return
	 * @throws Exception
	 */
	public String deleteBus() throws Exception {
		if(id != null){
			ywYnBusinfoService.delete(id);
			return "reloadDelBus";
		}
		if(busInfoId != null){
			ywYnBusinfoService.delete(busInfoId);
			return null;
		}
		return null;
	}
	
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = ywYnBusinfoService.get(id);
		}
		else{
			form = new YwYnBusinfoForm();
		}
		
	}
	

	public YwYnBusinfoForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwYnBusinfoForm> getPage() {
		return page;
	}


	public void setPage(Page<YwYnBusinfoForm> page) {
		this.page = page;
	}

	public String getAjid() {
		return ajid;
	}

	public void setAjid(String ajid) {
		this.ajid = ajid;
	}

	public Long getBusInfoId() {
		return busInfoId;
	}

	public void setBusInfoId(Long busInfoId) {
		this.busInfoId = busInfoId;
	}
	public String getShjg() {
		return shjg;
	}

	public void setShjg(String shjg) {
		this.shjg = shjg;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getQyid() {
		return qyid;
	}

	public void setQyid(String qyid) {
		this.qyid = qyid;
	}

	public String getSpsx() {
		return spsx;
	}

	public void setSpsx(String spsx) {
		this.spsx = spsx;
	}

	public String getBfyy() {
		return bfyy;
	}

	public void setBfyy(String bfyy) {
		this.bfyy = bfyy;
	}
	
	public YwYnBusinfoForm getForm() {
		return form;
	}

	public void setForm(YwYnBusinfoForm form) {
		this.form = form;
	}


}
